package com.cliente;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.util.Log;

import com.google.gson.Gson;

import domain.AdministradorVO;

public class AndroidController {
	
	private static final String URL ="http://http://172.22.195.184/:8080/WSAndroid/services/ServicioAndroid?wsdl";
	private static final String NAMESPACE= "http://webservice";
	
	public String getUrl() {
		return URL;
	}
	public String getNamespace() {
		return NAMESPACE;
	}
	
	public String logear(String user, String pass) {
		
		String METHOD_NAME = "logear";
		String SOAP_ACTION = NAMESPACE +"/" +METHOD_NAME;

		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

		// transformando a JSON
		domain.AdministradorVO admin = new AdministradorVO();
		admin.setUser(user);
		admin.setPassword(pass);
		final Gson gson = new Gson();
		String send = gson.toJson(admin);

		// agregando parametro
		request.addProperty("adminLoginJSON", send.toString());

		// configurando envio
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
		HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

		try {
			androidHttpTransport.call(SOAP_ACTION, envelope);
			SoapPrimitive response = (SoapPrimitive) envelope.getResponse();

			return response.toString();

		} catch (Exception e) {
			e.printStackTrace();
			Log.i(METHOD_NAME, " error: " + e);
		}

		return "Error de conexion con WS";
	}
	
	public boolean validToken(String token) {
		//esto es momentaneo OJO
		return (token.length() > 29);
	}
	
	public String getClientes(String clienteJson) {
		
		String METHOD_NAME = "mostrarClientes";
		String SOAP_ACTION = NAMESPACE +"/" +METHOD_NAME;
		Log.i(METHOD_NAME, " enviando: " + clienteJson);

		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
		request.addProperty("clienteJSON", clienteJson.toString());

		// configurando envio
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
		HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

		try {
			androidHttpTransport.call(SOAP_ACTION, envelope);
			SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
			Log.i("Webservice Output", response.toString());

			return response.toString();

		} catch (Exception e) {
			e.printStackTrace();
			Log.i(METHOD_NAME, " error: " + e);
		}

		return null;
	}
}


